package com.miyuan.view.machine;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.miyuan.R;
import com.miyuan.base.BaseFragment;
import com.miyuan.bean.ResultBean;
import com.miyuan.bean.request.MachineModifyParam;
import com.miyuan.bean.respone.MachineDetailsBean;
import com.miyuan.net.NetService;
import com.miyuan.net.RetrofitClient;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 详情修改
 */

public class ModifyDetialsFragment extends BaseFragment {
    @BindView(R.id.machine_code)
    EditText machineCode;
    @BindView(R.id.control_code)
    TextView controlCode;
    @BindView(R.id.machine_location)
    TextView machineLocation;
    @BindView(R.id.note)
    EditText note;

    String vendorId;
    MachineDetailsBean machineDetailsBean;

    public static ModifyDetialsFragment newInstance(Bundle args) {
        ModifyDetialsFragment modifyDetialsFragment = new ModifyDetialsFragment();
        modifyDetialsFragment.setArguments(args);
        return modifyDetialsFragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        setFragmentLayout(R.layout.fragment_modify_machine);
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Bundle bundle = getArguments();
        vendorId = bundle.getString("vendorId");
        machineDetailsBean = (MachineDetailsBean) bundle.getSerializable("machineDetailsBean");
        machineLocation.setText(machineDetailsBean.getPointName());
        machineCode.setText(machineDetailsBean.getNumber());
        controlCode.setText(machineDetailsBean.getControlNumber());
    }

//    @OnClick(R.id.modify_btn)
//    public void modify() {
//        machineCode.requestFocus();
//    }

    @OnClick(R.id.submit)
    public void submit() {
        String newCode = machineCode.getText().toString().trim();
        String noteString = note.getText().toString().trim();
        if (TextUtils.equals(newCode, machineDetailsBean.getNumber()) && TextUtils.isEmpty(noteString)) {
            Toast.makeText(getActivity(), "没有修改数据", Toast.LENGTH_SHORT).show();
            return;
        }
        if (vendorId != null && newCode != null) {
            RetrofitClient.getInstance()
                    .create(NetService.class)
                    .updateMachineDetials(vendorId, new MachineModifyParam(newCode, noteString).toRequestBody())
                    .enqueue(new Callback<ResultBean>() {

                        @Override
                        public void onResponse(Call<ResultBean> call, Response<ResultBean> response) {
                            ResultBean resultBean = response.body();
                            if (resultBean.code == 0) {
                                Toast.makeText(getActivity(), "修改成功", Toast.LENGTH_SHORT).show();
                                getActivity().finish();
                            } else {
                                Toast.makeText(getActivity(), resultBean.getMsg(), Toast.LENGTH_SHORT).show();
                            }
                        }

                        @Override
                        public void onFailure(Call<ResultBean> call, Throwable t) {

                        }
                    });
        }
    }

    @OnClick(R.id.back_layout)
    public void back() {
        InputMethodManager inputMethodManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(getActivity().getWindow().peekDecorView().getWindowToken(), 0);
        getFragmentManager().popBackStack();
    }

}
